package comp3021.a1;

import java.util.ArrayList;

public class ProductList extends Configuration{
		public int location;		
		protected ArrayList <Product> plist = new ArrayList<Product>();
		public ProductList()
		{
			
		}
		public ProductList(String fileName) 
		{
		 try{
			 java.io.File file = new java.io.File(fileName);
			 java.util.Scanner input = new java.util.Scanner(file);
		 // Read data from a file
	 		while(input.hasNextLine())
	 		{	
	 			Product p = new Product(input.next(),input.next(),input.nextFloat(),input.nextInt());
	 			plist.add(p);
	 			
	 		}
	 		input.close();
		 	}
		 catch(Exception ProductError)
		 {
			 System.out.println("Product data cannot be accessed");
		 }
		 
		 }
		
		public boolean alreadyAdd(Product p) {
			for(int i =0; i<plist.size(); i++)
			{
				if(plist.get(i).getKey().equals(p.getKey()))
				{
					return true;
				}
			}
			return false;
		}

		public int isExist(String ID)
		{	
			for( int i =0;i < plist.size(); i++)
			{
				if(plist.get(i).getKey().equals(ID) )
				{	
					location = i;
					return i;
				}
			}
			return -1;
		}
		public String itemName()
		{
			
			return plist.get(location).getName();
		}
		public float howmuch()
		{
			return plist.get(location).howmuch();
		}
		public boolean canBuy(int index, int number) {
			
			if( (plist.get(index).howmany() - number ) >=0)
			{
				return true;
			}
			return false;
		}	
	}
